package com.open.capacity.paper.controller;

import com.open.capacity.paper.service.OptionService;
import com.open.capacity.paper.vo.QuestionOptionVo;
import com.open.capacity.common.exception.controller.ControllerException;
import com.open.capacity.common.exception.service.ServiceException;
import com.open.capacity.common.model.Option;
import com.open.capacity.common.web.PageResult;
import com.open.capacity.common.web.Result;
import com.open.capacity.log.annotation.LogAnnotation;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@Api(tags = "选项管理")
@RestController
@RequestMapping("/option")
public class OptionController {

    @Autowired
    private OptionService optionService;

    /**
     * 模糊查询
     */
    @GetMapping("/getOptions")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "分页起始位置", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "limit", value = "分页结束位置", required = true, dataType = "Integer")
    })
    @ApiOperation(value = "后台管理查询所有的选项标识")
    @LogAnnotation(module = "paper-center")
    public PageResult<Option> getOptions(@RequestParam Map<String, Object> params) throws ControllerException {
        try {
            return optionService.getOptions(params);
        } catch (ServiceException e) {
            throw new ControllerException(e);
        }
    }

    /**
     * 逻辑删除
     */
    @DeleteMapping("/delete/{optnid}")
    @LogAnnotation(module = "paper-center")
    @ApiOperation(value = "后台管理删除选项标识")
    public Result delete(@PathVariable Integer optnid) throws ControllerException {
        try {
            return optionService.delete(optnid);
        } catch (Exception e) {
            throw new ControllerException(e);
        }
    }

    /**
     * 新增或更新
     */
    @PostMapping("/saveOrUpdate")
    @LogAnnotation(module = "paper-center")
    @ApiOperation(value = "后台管理新增或者更新选项标识,optionId==null?新增:更新")
    public Result saveOrUpdate(@RequestBody Option option) throws ControllerException {
        try {
            if (option.getId() != null) {
                return optionService.updateOption(option);
            } else {
                return optionService.saveOption(option);
            }
        } catch (Exception e) {
            throw new ControllerException(e);
        }
    }

    /**
     * 根据问题id获取所有选项
     */
    @GetMapping("/{quesid}/getAllOptions")
    @LogAnnotation(module = "paper-center")
    @ApiOperation(value = "后台管理根据问题id获取所有选项")
    public Result getOptionsByQuestionId(@PathVariable Integer questionId) throws ControllerException{
        try {
            return optionService.getOptionsByQuestionId(questionId);
        }catch (Exception e) {
            throw new ControllerException(e);
        }
    }

    /**
     * 添加问题选项
     */
    @PostMapping("/addOptions")
    @LogAnnotation(module = "paper-center")
    @ApiOperation(value = "后台管理给问题添加选项")
    public Result addOptions(@RequestBody QuestionOptionVo questionOptionVo) throws ControllerException{
        try {
            return optionService.addOptions(questionOptionVo.getQuestionId(),questionOptionVo.getOptionIds());
        } catch (Exception e) {
            throw new ControllerException(e);
        }
    }
}
